/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.servlet.admin;

import cn.eclass.bean.CategoryBean;
import cn.eclass.entity.CategoryEntity;
import cn.eclass.servlet.ActionFace;
import cn.eclass.utils.Helper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;

/**
 *
 * @author Administrator
 */
public class Category extends ActionFace {
    CategoryBean bean = new CategoryBean();    

    public void doRename() throws IOException {
        String name = Helper.toUTF8(request.getParameter("title"));
        CategoryEntity category = bean.getById(getNodeId());
        category.setName(name);
        bean.update(category);
        HashMap result = new HashMap();
        result.put("status", 1);
        result.put("id", getNodeId(category.getId()));
        out.print(toJsonWithNull(result));
    }

    public void doRemove() {
        Integer id = getNodeId();
        bean.remove(id);
        HashMap result = new HashMap();
        result.put("status", 1);
        result.put("id", id);
        out.print(toJsonWithNull(result));
    }

    public void doCreate() {
        String name = Helper.toUTF8(request.getParameter("title"));
        int parentId = getParentId();
        CategoryEntity category = new CategoryEntity();
        category.setName(name);
        category.setParentId(parentId);
        //reset category come from database;
        category = bean.insert(category);
        HashMap result = new HashMap();
        result.put("status", 1);
        result.put("id", getNodeId(category.getId()));
        out.print(toJsonWithNull(result));
    }

    public void doSelect() throws IOException {
        int rootId = getNodeId();
        List<CategoryEntity> categorys = bean.getByParentId(getNodeId());

        List nodes = new ArrayList();
        for (CategoryEntity category : categorys) {
            HashMap node = new HashMap();
            HashMap attr = new HashMap();

            node.put("data", category.getName());
            node.put("state", "closed");
            node.put("attr", attr);
            attr.put("id", getNodeId(category.getId()));

            nodes.add(node);
        }
        if (rootId <= 0) {
            List root = new ArrayList();
            HashMap rootNode = new HashMap();
            HashMap attr = new HashMap();
            
            rootNode.put("data", "分类目录");
            rootNode.put("state", "open");
            rootNode.put("attr", attr);
            attr.put("id", getNodeId(0));

            rootNode.put("children", nodes);
            root.add(rootNode);
            out.print(toJsonWithNull(root));
        } else {
            out.print(toJsonWithNull(nodes));
        }
    }

    private int getNodeId(String parameterName) {
        String nodeId = request.getParameter(parameterName);
        String[] nodeIdA = nodeId.split("_");
        nodeId = nodeIdA[1];
        return Integer.valueOf(nodeId);
    }

    private int getNodeId() {
        return getNodeId("id");
    }

    private String getNodeId(int id) {
        return "node_" + id;
    }

    private int getParentId() {
        return getNodeId("parent");
    }

    @Override
    public void doIndex() {
        try {
            request.getRequestDispatcher(adminView).forward(request, response);
        } catch (ServletException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
